<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;

class Users extends Authenticatable{
    use HasApiTokens;

    protected $table = 'users';

    const STATUS_1 = 1;
    const STATUS_2 = 2;
    const STATUS_3 = 3;
    const STATUS_4 = 4;

    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag  el-tag--info">待审核</span>',
        self::STATUS_2 => '<span class="el-tag">正常</span>',
        self::STATUS_3 => '<span class="el-tag el-tag--danger">驳回</span>',
    	self::STATUS_4 => '<span class="el-tag el-tag--danger">黑名单</span>'
    ];

    const SEX_1 = 1;
    const SEX_2 = 2;

    const SEX_TEXT = [
        self::SEX_1 => '<span class="el-tag">男</span>',
        self::SEX_2 => '<span class="el-tag el-tag--info">女</span>',
    ];

    const TYPE_1 = 1;
    const TYPE_2 = 2;
    const TYPE_3 = 3;

    const TYPE_TEXT = [
        self::TYPE_1 => '<span class="el-tag">赏金猎人</span>',
        self::TYPE_2 => '<span class="el-tag  el-tag--info">团长</span>',
    	self::TYPE_3 => '<span class="el-tag  el-tag--danger">悬赏主</span>'
    ];

    protected $fillable = [
        'id','true_name','mobile','nickname','avatar',
    	'grade','reward_num','balance','publish_num',
        'email','openid','sex','wechat_id',
        'login_at','sign_in_at','status','type','created_at','updated_at'
    ];


    protected $dates = ['login_at'];


    /**
     * 获取列表
     * @param array $params 条件参数1
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
            if(isset($params['kwd']) && $params['kwd']) {
                $query ->where('true_name','like','%' . $params['kwd'] . '%')
                    ->orWhere('nickname','like','%' . $params['kwd'] . '%')
                    ->orWhere('mobile','like','%' . $params['kwd'] . '%');
                  
            }
            if(isset($params['status']) && $params['status']) {
                $query ->where('status',$params['status']);
            }
            if(isset($params['type']) && $params['type']) {
                $query ->where('type',$params['type']);
            }
            if(isset($params['id']) && $params['id']) {
                $query ->whereNotIn('id',$params['id']);
            }
        })->where('status','<>',-1)->orderBy('created_at', 'desc')
            ->paginate($limit);
        $this->stateToText($lists,[
            'status' => self::STATUS_TEXT,
            'sex' => self::SEX_TEXT,
            'type' => self::TYPE_TEXT,
        ]);
        return $lists;
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function apiList($fields = ['*'],$limit){
        $lists = $this->select(array(
        \DB::raw('SUM(profit) as profit'),
        \DB::raw('SUM(order_count) as order_count')))->where('status','<>',-1)->where('status','<>',4)->orderBy('reward_num', 'desc')->orderBy('created_at','asc')->limit($limit)->get();
        return $lists;
    }

    /**
     * 获取用户详细信息
     * @param $data
     * @param array $map
     * @return mixed
     */
    protected function getUserInfo($params = [], $fields = ['*']){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['id']) && $params['id']) {
                    $query ->where('id',$params['id']);
                }
                if(isset($params['mobile']) && $params['mobile']) {
                    $query ->where('mobile',$params['mobile']);
                }
               
            })->where('status','<>',-1)->first();
        return $lists;
    }

    /**
     * 状态转换
     * @param $data
     * @param array $map
     * @return mixed
     */
    protected function stateToText(&$data, $map = []){
        foreach ($data as $key => &$row) {
            foreach ($map as $col => $pair) {
                if (isset($row[$col]) && isset($pair[$row[$col]])) {
                    $text = $col . '_text';
                    $row[$text] = $pair[$row[$col]];
                }
            }
        }
        return $data;
    }

    /**
     * passport默认使用email字段作为用户名，如果usename是其他字段必须重新定义findForPassport方法，
     * 且必须是public
     * @param $username
     * @return mixed
     */
    public function findForPassport($username)
    {
        return self::where('openid', $username)->first();
    }

    /**
     * 获取导出数据
     * @param array $params 条件参数
     * @return mixed
     */
    protected function exports($params,$type){
        $fields = [
             'nickname','true_name','mobile','avatar',
    		 'grade','reward_num','balance','publish_num',
        	
        	 'status','type','created_at',
        ];
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params) && $params) {
                    $ids = explode('-',$params);
                    $query ->whereIn('id',$ids);
                }
            })->where('status','<>',-1)->where('type',$type)->get();

        foreach ($lists as $k=>$v){
//           
            if($v->attributes['type'] == 1){
                $v->attributes['type'] = '赏金猎人';
                $v->attributes['publish_num'] = '/';
                $v->attributes['grade'] = UserLevels::where("id",$v->attributes['grade'])->value("name");
            }elseif($v->attributes['type'] == 2){
                $v->attributes['type'] = '团长';
                $v->attributes['publish_num'] = '/';
                $v->attributes['grade'] = UserLevels::where("id",$v->attributes['grade'])->value("name");
            }else{
            	$v->attributes['type'] = '悬赏主';
            	$v->attributes['reward_num'] = '/';
            	$v->attributes['grade'] = '/';
            }
            
            if($v->attributes['status'] == 1){
                $v->attributes['status'] = '待审核常';
            }elseif($v->attributes['status'] == 2){
                $v->attributes['status'] = '正常';
            }elseif($v->attributes['status'] == 3){
                $v->attributes['status'] = '驳回';
            }
            
            
            
        }
        return $lists;
    }
}
